package com.example.springworkadmin.controller;

import com.example.springworkadmin.dao.ResidentAccountDao;
import com.example.springworkadmin.entity.DTO.AfterResponseDTO;
import com.example.springworkadmin.entity.ResidentAccount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/residentAccount")
public class ResidentAccountController {
    @Autowired
    ResidentAccountDao residentAccountDao;

    @GetMapping("/getPage")
    public String getPage(){
        return "accountManage/residentAccount/residentAccountList";
    }

    @GetMapping("/addPage")
    public String addPage(){
        return "accountManage/residentAccount/addResidentAccount";
    }

    @GetMapping("/updatePage")
    public String updatePage(){
        return "accountManage/residentAccount/updateResidentAccount";
    }

    /**
     * 根据关键字获取居住人员（后台）
     * @param pages 当前页数
     * @param limit 页面大小
     * @param key 关键字
     * @return 资源列表list、资源总数count
     */
    @RequestMapping("/get")
    @ResponseBody
    public AfterResponseDTO get(@RequestParam(value = "page",defaultValue = "1")Integer pages,
                                @RequestParam(value = "limit",defaultValue = "10")Integer limit,
                                @RequestParam(value = "key",defaultValue = "")String key){
        int start = (pages-1)*limit;
        List<ResidentAccount> list=residentAccountDao.get(start,limit,key);
        int count=residentAccountDao.count(key);
        return new AfterResponseDTO(list,count,0);
    }

    /**
     * 添加居住人员（后台）
     * @param residentAccount 数据信息
     * @return code:0代表成功
     */
    @RequestMapping("/add")
    @ResponseBody
    public Map<String,Integer> add(ResidentAccount residentAccount){
        Integer flag=0;
        try {
            flag=residentAccountDao.add(residentAccount);
        }catch (Exception e){
            System.out.println("添加居住人员失败");
        }
        HashMap<String,Integer> map = new HashMap<>();
        map.put("code",flag-1);
        return map;
    }

    /**
     * 更新居住人员（后台）
     * @param residentAccount 数据信息
     * @return code:0代表成功
     */
    @RequestMapping("/update")
    @ResponseBody
    public Map<String,Integer> update(ResidentAccount residentAccount){
        Integer flag=0;
        try {
            flag=residentAccountDao.update(residentAccount);
        }catch (Exception e){
            System.out.println("更新居住人员失败");
        }
        HashMap<String,Integer> map = new HashMap<>();
        map.put("code",flag-1);
        return map;
    }

    /**
     * 删除居住人员（后台）
     * @param id 数据主键
     * @return code:0代表成功
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String,Integer> delete(@RequestParam("id") String id){
        Integer flag=0;
        try {
            residentAccountDao.deleteAccess(id);
            flag=residentAccountDao.delete(id);
        }catch (Exception e){
            System.out.println("删除居住人员失败");
        }
        HashMap<String,Integer> map = new HashMap<>();
        map.put("code",flag-1);
        return map;
    }
}
